<template>
  <div class="comment">
      <el-card>
          <template v-slot:header>
              <my-breadcrumb>评论管理</my-breadcrumb>
          </template>

            <!-- el-table-column 的内容定制
                    1.写prop,prop需要指定成遍历出的对象的属性
                    2.插槽
                    comment_status:true表示关闭了评论状态

                    通过作用域插槽，可以拿到行的数据 row
             -->
          <el-table :data="comments" v-loading='loading'>
              <el-table-column label="文章标题" prop="title"></el-table-column>
              <el-table-column label="总评论数" prop="total_comment_count"></el-table-column>
              <el-table-column label="粉丝评论数" prop="fans_comment_count"></el-table-column>
              <el-table-column label="评论状态">
                  <template v-slot:default="obj">
                      {{ obj.row.comment_status ? '关闭' : '正常' }}
                  </template>
              </el-table-column>
              <el-table-column label="操作">
                  <template v-slot:default='obj'>
                      <el-button @click="toggleComment(obj.row)" v-if="obj.row.comment_status" type="success">打开评论</el-button>
                     <el-button @click="toggleComment(obj.row)" v-else type="danger">关闭评论</el-button>
                  </template>
              </el-table-column>
          </el-table>

          <el-pagination
            @current-change='handleCurrentChange'
            background
            layout="prev, pager, next"
            :page-size="per_page"
            :current-page="page"
            :total="total">
        </el-pagination>
      </el-card>
  </div>
</template>

<script>
import myBreadcrumb from '@/components/my-breadcrumb.vue'
import { reqGetComments, reqModCommentStatus } from '@/api/comment.js'
export default {
  name: 'Comment',
  components: {
    myBreadcrumb
  },
  data () {
    return {
      page: 1, // 当前页
      per_page: 10, // 每页条数
      comments: [], // 评论列表的数据
      total: 0, // 总条数
      loading: false
    }
  },
  created () {
    this.getComments()
  },
  methods: {
    async getComments () {
      this.loading = true
      try {
        const res = await reqGetComments(this.page, this.per_page)
        this.comments = res.data.data.results
        this.total = res.data.data.total_count
      } catch (e) {
        console.log(e)
      }
      this.loading = false
    },
    handleCurrentChange (index) {
      this.page = index
      this.getComments()
    },
    async toggleComment (row) {
      console.log(row)
      try {
        await reqModCommentStatus(row.id.toString(), !row.comment_status)
        // console.log(res)
        this.$message.success('操作成功')
        this.getComments()
      } catch (err) {
        this.$message.error('操作失败')
        console.log(err)
      }
    }
  }
}
</script>

<style>

</style>
